/*
 * Copyright (c) $today.year-Now http://www.zxpnet.com All rights reserved.
 */

package com.zxp.security.config;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;

import org.beetl.core.Context;
import org.beetl.core.Function;
import org.beetl.core.GroupTemplate;
import org.beetl.ext.spring.EnableBeetl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * 参考springboot-plus  com.ibeetl.admin.core.conf.BeetlConf
 * @author: shollin
 * @date: 2021/7/6/006 9:07
 */
@Configuration
@EnableBeetl
public class MyBeetlConfig {

    @Autowired
    Environment env;

    /*@Bean
    public BeetlTemplateCustomize beetlTemplateCustomize() {
        return new BeetlTemplateCustomize() {
            public void customize(GroupTemplate groupTemplate) {

                //groupTemplate.registerFunction("uuid", new UUIDFunction());

                // 对象工具类
                groupTemplate.registerFunctionPackage("objectUtil", ObjectUtil.class);

                groupTemplate.registerFunctionPackage("uuid", UUID.class);
                groupTemplate.registerFunctionPackage("strUtil", StrUtil.class);

                // 模板页面判断是否有按钮权限,比如canAccess


                groupTemplate.registerFunction("env", new Function() {
                    @Override
                    public String call(Object[] paras, Context ctx) {
                        String key = (String)paras[0];
                        String value =  env.getProperty(key);
                        if(value!=null) {
                            return getStr(value);
                        }
                        if(paras.length==2) {
                            return (String)paras[1];
                        }
                        return null;
                    }

                    protected String getStr(String str) {
                        try {
                            return new String(str.getBytes("iso8859-1"), Charsets.UTF_8);
                        } catch (UnsupportedEncodingException e) {
                            throw new RuntimeException(e);
                        }
                    }

                });
            }
        };
    }*/


}
